package com.jlg.util;

import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
*
* author: huazhang
* since : 2011-3-26
*/
public class Utils
{
    public static Pattern chinesePattern = Pattern.compile("[\u4e00-\u9fa5]+");  
    
    public static String getProtocal(String url)
    {
        try
        {
            return new URL(url).getProtocol();
        } catch (MalformedURLException e)
        {
            return "";
        }
    }
    
    public static String getDomain(String url)
    {
        try
        {
            return new URL(url).getProtocol() + "://" + new URL(url).getHost();
        } catch (MalformedURLException e)
        {
            return "";
        }
    }
    
    //对URL中的中文进行编码
    public static String encodeUrl(String sUrl)
    {
        try
        {
            StringBuffer sb = new StringBuffer();
            Matcher m = chinesePattern.matcher(sUrl);
            boolean b = m.find();
            int last = 0;
            while (b)
            {
                m.appendReplacement(sb, URLEncoder.encode(m.group(), "utf-8"));
                last = m.end();
                b = m.find();
            }
            if (sUrl.length() > last)
                sb.append(sUrl.substring(last));
            if (sb != null && sb.length() > 0)
                sUrl = sb.toString();
        } catch (Exception e)
        {
        }
        return sUrl;
    }
    
    public static boolean isNumeric(String str)
    {
        for (int i = str.length(); --i >= 0;)
        {
            if (!Character.isDigit(str.charAt(i)))
            {
                return false;
            }
        }
        return true;
    }

    public static void info(String out){
        System.out.println(out);
    }
    
    public static void ERR(String cause){
        System.err.println(cause);
    }

    public static void ERR(Exception e)
    {
        e.printStackTrace();
    }
    
    public static long parseLong(String str, long defaultValue)
    {
        long result = -1;
        try
        {
            result = Long.parseLong(str);
        } catch (Exception e)
        {
            result = defaultValue;
        }
        return result;
    }
    
    public static int parseInt(String str, int defaultValue)
    {
        int result = -1;
        try
        {
            result = Integer.parseInt(str);
        } catch (Exception e)
        {
            result = defaultValue;
        }
        return result;
    }
    
    public static boolean isEmpty(String str)
    {
        if (str == null || "".equals(str.trim()))
        {
            return true;
        }
        return false;
    }
    
    
    public static boolean isEmpty(String[] arr)
    {
        if (arr == null || arr.length==0)
        {
            return true;
        }
        return false;
    }
    
    public static boolean isEmpty(Map map)
    {
        if (map == null || map.size()==0)
        {
            return true;
        }
        return false;
    }
    
    public static void safeClose(Statement stmt)
    {
        try
        {
            if (stmt != null)
            {
                stmt.close();
            }
        }
        catch (SQLException e)
        {
            ERR(e);
        }
    }
    
    public static void safeClose(Connection conn)
    {
        try
        {
            if (conn != null)
            {
                conn.close();
            }
        }
        catch (SQLException e)
        {
            ERR(e);
        }
    }

    public static void safeClose(ResultSet rs)
    {
        try
        {
            if (rs != null)
            {
                rs.close();
            }
        }
        catch (SQLException e)
        {
            ERR(e);
        }
    }

    public static void safeRollback(Connection conn)
    {
        if (conn == null)
        {
            return;
        }

        try
        {
            conn.rollback();
        }
        catch (SQLException e)
        {
            ERR(e);
        }
    }

    public static void safeClose(InputStream is)
    {
        try
        {
            if (is != null)
            {
                is.close();
            }
        }
        catch (Exception e)
        {
            ERR(e);
        }
    }

    public static void safeClose(OutputStream os)
    {
        try
        {
            if (os != null)
            {
                os.close();
            }
        }
        catch (Exception e)
        {
            ERR(e);
        }
    }

    public static void safeClose(Reader reader)
    {
        try
        {
            if (reader != null)
            {
                reader.close();
            }
        }
        catch (Exception e)
        {
            ERR(e);
        }
    }

    public static void safeClose(Writer writer)
    {
        try
        {
            if (writer != null)
            {
                writer.close();
            }
        }
        catch (Exception e)
        {
            ERR(e);
        }
    }
}
